package com.mojo.concurrent.lock;

import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport类详解
 * <pre>
 * LockSupport是提供锁机制的工具类。
 * 核心函数：park(),unpark()
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2023, 广州九尾信息科技有限公司
 * @date 2023/1/5 15:09
 */
public class LockSupportDemo {
    public static void main(String[] args) {
        Thread t = Thread.currentThread();
        Bank bank = new Bank(t);
        bank.start();
        System.out.println(">>>before park");
        LockSupport.park("park thread");
        System.out.println(">>>after park");
    }
}

class Bank extends Thread {
    private final Object object;

    public Bank(Object object) {
        this.object = object;
    }

    @Override
    public void run() {
        //2、上锁
        System.out.println(">>>>before unpark");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("blocker" + LockSupport.getBlocker((Thread) object));
        LockSupport.unpark((Thread) object);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("blocker" + LockSupport.getBlocker((Thread) object));
        System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
        System.out.println(">>>>after unpark");
    }

}